
package com.xebest.web.controller.aftersales;

import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.util.DateUtil;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.XeVenExcelUtil;
import com.xebest.util.msg.XeVenAfterSaleMsg;
import com.xebest.web.controller.export.XeVenAfterSaleExportDTO;
import com.xebest.web.form.XeVenAfterSaleQueryForm;
import com.xebest.web.service.XeVenAfterSaleCenterExportService;
import com.xebest.web.view.ErpUserInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("afterSaleCenterExport")
public class XeVenAfterSaleCenterExportController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenAfterSaleController.class);

    //客服中心业务处理
    @Autowired
    private XeVenAfterSaleCenterExportService afterSaleCenterExportService;

    //导出文件目录
    @Value("${xeven.erp.exportTempletDir}")
    private String exportTempletDir;

    /**
     * 导出售后单列表
     *
     * @author 赵盼飞
     */
    @RequestMapping("afterSaleList")
    public @ResponseBody
    BaseResponse queryAfterSaleExportList(@ModelAttribute XeVenAfterSaleQueryForm form, HttpServletResponse response) {

        try {
            String[] str = new String[]{"afterSalesCode", "afterSaleSource", "createDate", "afterSaleType",
                    "afterSaleReason", "afterSaleRemark", "storeName", "afterSaleStatus", "serviceCheckStatus",
                    "purchaseCheckStatus", "financeCheckStatus", "orderId", "sendStoreName", "loginAccount", "caConsignee",
                    "epayReceAmtDiff", "freAmount", "epayReceAmt", "refundAmount"};
            form.setFieldArr(str);
            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            String createStartDate = form.getCreateStartDate();
            String createEndDate = form.getCreateEndDate();
            //设定查询中日期为3个月以内
            if (StringUtils.isNotBlank(createStartDate) && StringUtils.isNotBlank(createEndDate)) {
                Boolean dateCm = DateUtil.comparableTime(createStartDate, createEndDate);
                if (!dateCm) {
                    return sendAjaxError(XeVenAfterSaleMsg.AFTER_SALE_DATE_COMPARABLE);
                }
            }
            if (StringUtils.isBlank(createStartDate) && StringUtils.isBlank(createEndDate)) {
                createEndDate = DateUtil.getDate(new Date());
                ;
                createStartDate = DateUtil.strToNMonth(createEndDate, -3);
            }
            if (StringUtils.isBlank(createStartDate) && StringUtils.isNotBlank(createEndDate)) {
                form.setCreateStartDate(DateUtil.strToNMonth(createEndDate, -3));
            }
            if (StringUtils.isBlank(createEndDate) && StringUtils.isNotBlank(createStartDate)) {
                form.setCreateEndDate(DateUtil.strToNMonth(createStartDate, 3));
            }
            if (StringUtils.isNotBlank(form.getFormIdType())) {
                int formIdType = Integer.parseInt(form.getFormIdType());
                switch (formIdType) {
                    case 1:   //售后单号
                        form.setAfterSalesCode(form.getFormIdValue());
                        break;
                    case 2:   //订单编号,
                        form.setOrderInfo(form.getFormIdValue());
                        break;
                    case 3:   //商品名称
                        form.setProName(form.getFormIdValue());
                        break;
                    case 4:   //商品货号
                        form.setProNum(form.getFormIdValue());
                        break;
                    case 5:   //客户账号
                        form.setUserId(form.getFormIdValue());
                        break;
                    case 6:   // 收货人
                        form.setCaConsignee(form.getFormIdValue());
                        break;
                    default:
                        break;
                }

            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            //2.获取数据
            List<XeVenAfterSaleExportDTO> afterSaleExportList = afterSaleCenterExportService.queryAfterSaleExportList(form);
            if (null != afterSaleExportList && afterSaleExportList.size() > 0) {
                //3.导出
                boolean rs = XeVenExcelUtil.exportExcelService(afterSaleExportList, new XeVenAfterSaleExportDTO(), form.getFieldArr(), exportTempletDir, "AFTERSALELIST-", response);
                if (rs) {
                    return sendAjaxOK();
                } else {
                    return sendAjaxError();
                }
            } else {
                return sendAjaxOK("导出数据为空！");
            }
        } catch (Exception e) {
            log.error("导出售后单列表接口异常：{}", e);
            return sendAjaxError();
        }
    }


    /**
     * 导出售后单明细
     *
     * @author 赵盼飞
     */
    @RequestMapping("afterSaleExportDetail")
    public @ResponseBody
    BaseResponse queryAfterSaleExportDetail(@ModelAttribute XeVenAfterSaleQueryForm form, HttpServletResponse response) {

        try {
            String[] str = new String[]{"afterSalesCode", "afterSaleSource", "createDate", "afterSaleType",
                    "afterSaleReason", "afterSaleRemark", "storeName", "afterSaleStatus", "serviceCheckStatus",
                    "purchaseCheckStatus", "financeCheckStatus", "orderId", "sendStoreName", "loginAccount", "caConsignee",
                    "proNum", "proName", "proOrgCodeName", "pricingType", "proUnit", "specValue", "proPrice",
                    "purchaseNum", "proTotalPrice", "pmtAmount", "specialDisamount", "realSendNum",
                    "realSendAmount", "rejectNum", "rejectAmount"};
            form.setFieldArr(str);
            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            String createStartDate = form.getCreateStartDate();
            String createEndDate = form.getCreateEndDate();
            //设定查询中日期为3个月以内
            if (StringUtils.isNotBlank(createStartDate) && StringUtils.isNotBlank(createEndDate)) {
                Boolean dateCm = DateUtil.comparableTime(createStartDate, createEndDate);
                if (!dateCm) {
                    return sendAjaxError(XeVenAfterSaleMsg.AFTER_SALE_DATE_COMPARABLE);
                }
            }
            if (StringUtils.isBlank(createStartDate) && StringUtils.isBlank(createEndDate)) {
                createEndDate = DateUtil.getDate(new Date());
                ;
                createStartDate = DateUtil.strToNMonth(createEndDate, -3);
            }
            if (StringUtils.isBlank(createStartDate) && StringUtils.isNotBlank(createEndDate)) {
                form.setCreateStartDate(DateUtil.strToNMonth(createEndDate, -3));
            }
            if (StringUtils.isBlank(createEndDate) && StringUtils.isNotBlank(createStartDate)) {
                form.setCreateEndDate(DateUtil.strToNMonth(createStartDate, 3));
            }
            if (StringUtils.isNotBlank(form.getFormIdType())) {
                int formIdType = Integer.parseInt(form.getFormIdType());
                switch (formIdType) {
                    case 1:   //售后单号
                        form.setAfterSalesCode(form.getFormIdValue());
                        break;
                    case 2:   //订单编号,
                        form.setOrderInfo(form.getFormIdValue());
                        break;
                    case 3:   //商品名称
                        form.setProName(form.getFormIdValue());
                        break;
                    case 4:   //商品货号
                        form.setProNum(form.getFormIdValue());
                        break;
                    case 5:   //客户账号
                        form.setUserId(form.getFormIdValue());
                        break;
                    case 6:   // 收货人
                        form.setCaConsignee(form.getFormIdValue());
                        break;
                    default:
                        break;
                }

            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            //2.获取数据
            List<XeVenAfterSaleExportDTO> afterSaleExportList = afterSaleCenterExportService.queryAfterSaleExportDetail(form);
            if (null != afterSaleExportList && afterSaleExportList.size() > 0) {
                //3.导出
                boolean rs = XeVenExcelUtil.exportExcelService(afterSaleExportList, new XeVenAfterSaleExportDTO(), form.getFieldArr(), exportTempletDir, "AFTERSALEDETAIL-", response);
                if (rs) {
                    return sendAjaxOK();
                } else {
                    return sendAjaxError();
                }
            } else {
                return sendAjaxOK("导出数据为空！");
            }
        } catch (Exception e) {
            log.error("导出售后单明细接口异常：{}", e);
            return sendAjaxError();
        }
    }


}
  
